package cn.edu.neusoft.yuejian.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.neusoft.yuejian.R;
import cn.edu.neusoft.yuejian.bean.user;
import cn.edu.neusoft.yuejian.model.usermodel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegActivity extends AppCompatActivity {
    private TextView reg_user,reg_pas;
    private Button reg_but;
    private ImageButton reg_back;
    private user user;
    private String userid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initViews();
        initEvents();
        initData();
    }
    private void initViews() {
        reg_user=(TextView)findViewById(R.id.reg_edit_user);
        reg_pas=(TextView)findViewById(R.id.reg_edit_pas);
        reg_but=(Button)findViewById(R.id.reg_button);
        reg_back=(ImageButton)findViewById(R.id.reg_imageButton4);

    }


    private void initEvents() {
       reg_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegActivity.this,
                        LoginActivity.class);
                RegActivity.this.startActivity(intent);
            }
        });
        reg_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                usermodel usermodel=new usermodel();
                Call<user> call=usermodel.Reg(reg_user.getText().toString(),reg_pas.getText().toString());
                call.enqueue(new Callback<user>() {
                    @Override
                    public void onResponse(Call<user> call, Response<user> response) {
                        if (response.isSuccessful()){
                            user = response.body();
                            userid = user.getId();
                            if (!userid.equals("0")){

                                Intent intent = new Intent(RegActivity.this,
                                        LoginActivity.class);

                                RegActivity.this.startActivity(intent);
                                finish();
                            }else{
                                Toast.makeText(RegActivity.this,
                                        "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }

                    @Override
                    public void onFailure(Call<user> call, Throwable t) {
                        Toast.makeText(RegActivity.this,
                                "与服务器通讯失败，请检查网络设置！",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }


    private void initData() {

    }
}
